package com.stores.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.stores.action.ManageBrandAction;
import com.stores.action.ManageManufacturerAction;
import com.stores.model.Brand;
import com.stores.toolset.InputValidator;
import com.stores.toolset.WebAction;

public class BrandsController extends HttpServlet {

	private static final String ADMIN_BRAND_SAVE = "/admin/brand/save";
	private static final String ADMIN_BRAND_LOAD = "/admin/brand/load";
	private static final String ADMIN_BRAND_LOAD_MFG = "/admin/brand/load/mfg";
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/***
	 * 
	 */
	@SuppressWarnings("static-access")
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		ServletContext context = getServletContext();
		String requestUrl = request.getRequestURL().toString();
		if (requestUrl.indexOf("manage-brands") > -1) {
			RequestDispatcher dispatcher = context
					.getRequestDispatcher("/admin/manage-brand.jsp");
			try {
				dispatcher.forward(request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			}
		} else {
			try {
				response.sendError(response.SC_BAD_REQUEST,
						"Valid url is /admin/manage-brands");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			}
		}
	}

	/***
	 * 
	 */
	@SuppressWarnings("static-access")
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		
		ManageBrandAction manage = new ManageBrandAction();
		InputValidator validator = new InputValidator();
		WebAction webAction = new WebAction(request, response);

		if (webAction.isUrl(ADMIN_BRAND_LOAD_MFG)) {
			Gson gson = new Gson();
			webAction.writeResponse(gson
					.toJson((new ManageManufacturerAction()).Retrive()));
		} else if (webAction.isUrl(ADMIN_BRAND_LOAD)) {
			Gson gson = new Gson();
			webAction.writeResponse(gson.toJson(manage.Retrive()));
		} else {
			String description = request.getParameter("brand-desc");
			String name = request.getParameter("brand-name");
			String mfgId = request.getParameter("mfg-list");

			if (validator.isValid(description + name + mfgId)) {// club them
				// together
				// and
				// send for
				// validation
				Brand brand = new Brand();
				brand.setDescription(description);
				brand.setName(name);
				brand.setManufacturerId(Long.parseLong(mfgId));
				if (webAction.isUrl(ADMIN_BRAND_SAVE)) {
					String categoryId = request.getParameter("brand-id");
					if (categoryId.length() == 0) {
						manage.Create(brand);
					} else {
						brand.setId(Long.parseLong(categoryId));
						manage.Update(brand);
					}
				}
			} else {
				try {
					response.sendError(response.SC_BAD_REQUEST,
							"Invalid characters found in the input data. Data validation failed!!");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					// e.printStackTrace();
				}
			}
		}
	}
}
